﻿Public Class FrmExamenADD
    Public pType_exam As String

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim myDataset As New DataSet    'Objet DataSet
    Dim myDataTable As DataTable    'Objet DataTable
    Dim myAdapter As New Odbc.OdbcDataAdapter


    Private Sub FrmExamenINS_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        InitComboLieux(CB_LIEUX, "EX")
        InitComboSocietes(CB_SOCIETES)

        ClearTextBoxes(Me)
        TE_DATE.Value = Now()
        CB_LIEUX.SelectedIndex = CB_LIEUX.FindString("SAINT PRIEST")
        TE_ADRESSE.Text = getAdresse(CB_LIEUX.SelectedItem.ToString)
        If pType_exam = "ETG" Then
            TE_HEURE.Value = New Date(Now.Year, Now.Month, Now.Day, 7, 55, 0)
        Else
            TE_HEURE.Value = New Date(Now.Year, Now.Month, Now.Day, 8, 0, 0)
        End If

        ' rempli la liste des Agences
        myDataset.Clear()

        ' effacer le contenu du Datagrid
        DG_Codes.DataSource = Nothing
        DG_Codes.Rows.Clear()
        DG_Codes.Refresh()

        ' définir la table comme source de donnée
        myConnect.ConnectionString = ctDSN_AEM
        myCommand.CommandText = "SELECT libelle as Agence,'' as Total FROM P_AGENCES ORDER BY code"
        myCommand.Connection = myConnect
        myAdapter.SelectCommand() = myCommand

        ' lier le Dataset à la source de donnée
        myAdapter.Fill(myDataset, "P_AGENCES")
        ' remplir la DataTable par le Dataset
        myDataTable = myDataset.Tables("P_AGENCES")
        ' puis l'afficher dans le Datagrid
        DG_Codes.DataSource = myDataTable
        myConnect.Close()

        ' Définir la colonne identifiant comme ReadOnly pour empecher la maj
        DG_Codes.Columns(0).ReadOnly = True
        DG_Codes.DefaultCellStyle.SelectionBackColor = Color.Gray
        DG_Codes.DefaultCellStyle.BackColor = Color.Ivory
        DG_Codes.AllowUserToAddRows = False
        ' rendre visible
        DG_Codes.Visible = True

    End Sub

    Private Sub BO_SAVE_Click(sender As System.Object, e As System.EventArgs) Handles BO_SAVE.Click
        Dim nAgence As Integer
        Dim nTotal As Integer

        nAgence = 0
        For Each row As DataGridViewRow In DG_Codes.Rows
            If Not row.IsNewRow Then
                nAgence += 1
                If row.Cells(1).Value.Length And IsNumeric(row.Cells(1).Value) Then
                    nTotal = CInt(row.Cells(1).Value)
                    ' si total > 0, créer  un enregistrement Examen 
                    szSQL = "CALL spUpd_EXAMENS('" & pType_exam & "','',0,0,'" & _
                             Format(TE_DATE.Value, "yyyy/MM/dd") & "','" & Format(TE_HEURE.Value, "HH:mm") & "'," & _
                             TE_NO_EXA.Value & ",'" & TE_CLE.Text & "','" & CB_LIEUX.SelectedItem.ToString & "','" & _
                             TE_ADRESSE.Text & "','" & Mid(CB_SOCIETES.SelectedItem.ToString, 1, 1) & "'," & nAgence & "," & _
                             nTotal & ",0,'" & gUserCode & "')"
                    ExecQuery(ctDSN_AEM, szSQL)
                End If
            End If

        Next
        Me.Close()

    End Sub

    Private Sub CB_LIEUX_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles CB_LIEUX.SelectedValueChanged
        TE_ADRESSE.Text = getAdresse(CB_LIEUX.SelectedItem.ToString)
    End Sub
End Class